package com.lakeworks.service.impl;

import com.lakeworks.common.core.domain.AjaxResult;
import com.lakeworks.common.domain.channel.PickUpStatus;
import com.lakeworks.common.domain.channel.ProductInfo;
import com.lakeworks.common.enums.PickupType;
import com.lakeworks.common.enums.StatusRecordEnum;
import com.lakeworks.domain.OprPickup;
import com.lakeworks.domain.vo.PickupVo;
import com.lakeworks.mapper.OprMerchandiseMapper;
import com.lakeworks.mapper.OprPickupMapper;
import com.lakeworks.mapper.OprProductMapper;
import com.lakeworks.service.IOprPickupService;
import com.lakeworks.service.api.MerchantAsync;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 入厂单Service业务层处理
 *
 * @author lake
 * @date 2023-02-08
 */
@Service
public class OprPickupServiceImpl implements IOprPickupService {
    @Resource
    private OprPickupMapper oprPickupMapper;

    @Autowired
    private MerchantAsync merchantAsync;

    @Resource
    private OprMerchandiseMapper merchandiseMapper;

    @Resource
    private OprProductMapper oprProductMapper;

    @Override
    public int countByExpressNo(String expressNo) {
        return oprPickupMapper.selectByExpressNo(expressNo);
    }

    @Override
    public OprPickup selectByChannelIdAndChPickupNo(Long id, String chPickupNo) {
        return oprPickupMapper.queryByChannelIdAndChPickupNo(id, chPickupNo);
    }

    @Override
    public List<ProductInfo> pickupInfoById(Long id) {
        return oprPickupMapper.pickupInfoById(id);
    }

    @Override
    public ProductInfo productInfoById(Long id, String barcode) {
        return oprPickupMapper.productInfoById(id, barcode);
    }

    @Override
    public String selectOrderNoByBarcode(String barcode) {
        return oprPickupMapper.selectOrderNoByBarcode(barcode);
    }

    /**
     * 查询入厂单
     *
     * @param id 入厂单主键
     * @return 入厂单
     */
    @Override
    public OprPickup selectOprPickupById(Long id) {
        return oprPickupMapper.selectOprPickupById(id);
    }

    /**
     * 查询入厂单列表
     *
     * @param oprPickup 入厂单
     * @return 入厂单
     */
    @Override
    public List<OprPickup> selectOprPickupList(OprPickup oprPickup) {
        return oprPickupMapper.selectOprPickupList(oprPickup);
    }

    @Override
    public int insertOprPickup(OprPickup oprPickup) {
        int flag = oprPickupMapper.insertOprPickup(oprPickup);
        if (flag == 0)
            throw new RuntimeException("入厂单保存失败");
        return flag;
    }

    /**
     * 修改入厂单
     *
     * @param oprPickup 入厂单
     * @return 结果
     */
    @Override
    public int updateOprPickup(OprPickup oprPickup) {
        int flag = oprPickupMapper.updateOprPickup(oprPickup);
        if (flag == 0)
            throw new RuntimeException("入厂单更新失败");
        return flag;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult signFor(String expressNo, boolean flag) {
        OprPickup pickup = oprPickupMapper.selectPickByExpressNo(expressNo);
        if (pickup == null) {
            return AjaxResult.warn("找不到入场单信息");
        }
        if(PickupType.CANCEL.getValue().equals(pickup.getStatus())){
            return AjaxResult.error("订单已取消");
        }
        pickup.setExpressNo(expressNo);
        pickup.setStatus(PickupType.SIGNFOFF.getValue());
        oprPickupMapper.updatePickupStatus(pickup);
        //保存入场单已签收状态改变记录
        OperateRecordUtil.commonImportRecord(StatusRecordEnum.PICKUP, pickup.getId(), pickup.getStatus());
        //给渠道回写入场单已签收状态
        merchantAsync.backPickState(PickUpStatus.builder()
                .pickupNo(pickup.getChPickupNo())
                .status(PickupType.instance(pickup.getStatus()).toString())
                .build(), pickup.getChannelId());
        return AjaxResult.success();
    }

    @Override
    public void pickupCancel(Long pickupId) {
        merchandiseMapper.pickupCancel(pickupId);
        oprProductMapper.pickupCancel(pickupId);
    }

    @Override
    public PickupVo selectPickupVoById(Long id) {
     return oprPickupMapper.selectPickupVoById(id);
    }
}
